<template>
	<!-- 地址管理 -->
	<view class="address public">
		<view class="main">
			 <!-- :index="index" @open="open" -->
			<u-swipe-action :show="item.show" v-for="(item, index) in arr.list" :key="item.id"
				@click="click(item.id)" :options="options">
				<!-- v-for="item in arr.list" :key="item.id" -->
				<view class="main-item" bindlongpress="handleLongPress">
					<view class="main-item-left" @click="source == 0 ? Returntopreviouspage(item) : ''">
						<view class="main-item-left-address">
							{{item.address}} {{item.house_number}}
							<!-- {{ item.house_number }} -->
						</view>
						<view class="main-item-left-information">
							<text>{{item.name}}</text>
							<text>{{item.mobile}}</text>
						</view>
					</view>
					<view class="main-item-right" @click="JumpAddress(1,item)">
						<!-- <image :src="item.icon" mode=""></image> -->
						<image src="../../../static/my/giveServiceTo/edit.png" mode=""></image>
					</view>
				</view>
			</u-swipe-action>

		</view>
		<view class="footer">
			<button @click="JumpAddress(2)">新增收货地址</button>
		</view>
	</view>
</template>

<script>
	const $api = require('@/utils/request.js').API
	export default {
		data() {
			return {
				arr: [],
				// disabled: false,
				// btnWidth: 180,
				show: false,
				source: '',
				options: [
					// {
					// 	text: '收藏',
					// 	style: {
					// 		backgroundColor: '#007aff'
					// 	}
					// },
					{
						id:1,
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			}
		},
		onLoad(options) {
			this.source = options.source
		},
		onShow() {
			this.postAddressList()
		},
		methods: {
			click(id) {
				uni.showModal({
					title:'您确定要删除该地址吗？',
					success:(res) => {
						if(res.confirm) {
							let obj = {
								id:id,
								token: uni.getStorageSync('token'),
							}
							$api.postAddressDel(obj).then(res => {
								uni.showToast({
									title:'删除成功',
									duration:2000
								})
								this.postAddressList()
							})
						} else if(res.cancel) {
							uni.showToast({
								title:'取消删除',
								icon:'none',
								duration:2000
							})
						}
					}
				})
				
			},
			postAddressList() {
				let obj = {
					token: uni.getStorageSync('token')
				}
				$api.postAddressList(obj).then(res => {
					if (res.data.code == 0) {
						this.arr = res.data.data
					}
				})
			},
			// 跳转地址
			JumpAddress(e, item) {
				if (e == 1) {
					uni.navigateTo({
						url: `../giveServiceTo/NewAddress?status=${1}&item=${JSON.stringify(item)}`
					})
				} else if (e == 2) {
					uni.navigateTo({
						url: '../giveServiceTo/NewAddress?status=' + 2
					})
				}
			},
			Returntopreviouspage(item){
				// uni.navigateBack({
				// 	delta:1,
				// 	success: function() {
				// 	},
				// 	fail: function() {}
				// })
				uni.$emit('returnAddress',item.id)
				uni.setStorage({
					key:'address',
					data:item.id,
				})
				uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1,
					// success: function() {
					// 	uni.postMessage({
					// 		data: item.id
					// 	})
					// }
				});
			},
		}
	}
</script>

<style lang="less" scoped>
	.address {
		position: relative;
		// width: 100%;
		// height: 93vh;
		font-family: PingFang SC;
		background: #F3F3F3;
		.main {
			width: 750rpx;
			height: 100vh;
			overflow: hidden;
			overflow: scroll;
			.main-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1px solid #F3F3F3;
				background: white;
				padding: 0 55rpx 0 44rpx;

				.main-item-left {
					flex-grow: 1;
					.main-item-left-address {
						font-size: 32rpx;
						font-weight: 500;
						color: #262626;
						line-height: 40rpx;
						padding-top: 36rpx;
					}

					.main-item-left-information {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #A9A9A9;
						line-height: 30rpx;
						padding: 35rpx 0;
						text:last-child {
							padding-left: 15rpx;
						}
					}
				}

				.main-item-right {
					image {
						width: 37rpx;
						height: 43rpx;
					}
				}
			}
		}

		.footer {
			position: absolute;
			width: 630rpx;
			height: 88rpx;
			left: 60rpx;
			bottom: 20rpx;
			margin: auto;

			button {
				font-size: 30rpx;
				font-weight: 400;
				color: #FFFFFF;
				background: #F14500;
				border-radius: 44rpx;
			}
		}
	}
</style>